package org.itboys.mobile.controller.weixin.member;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.util.MobileSignUtils;
import org.itboys.mobile.dto.member.SundayMemberDto;
import org.itboys.mobile.dto.member.SundayWeixinMemberDto;
import org.itboys.mobile.service.mongo.weixin.member.SundayWeixinMemberService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.HttpRequestUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created on 2017/12/7.
 * @author 朱坤涛
 */
@Controller
@RequestMapping("/sunday/weixin/member")
public class SundayWeixinMemberController extends BaseController{

    @Autowired
    private SundayWeixinMemberService memberService;

    /**
     * 用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第第二步。
     * @param request
     * @param response
     * @param thirdType
     * @param unionId
     */
    @RequestMapping(value="/loginWithThirdOne", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第二步。")
    public void loginWithThirdOne(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId){

        try{

            SundayWeixinMemberDto member= memberService.loginWithThirdOne(thirdType, unionId);
            if(member==null){
                ResponseMessage.error(-2,"需要验证手机号");
            }else{
                ResponseMessage.success(member);
            }

        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     */

    @RequestMapping(value="/loginWithThirdTwo", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。")
    public void loginWithThirdTwo(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code){

        try{
            SundayWeixinMemberDto member= memberService.loginWithThirdTwo( thirdType, nickName, image,  unionId,  mobile, code);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }


    /** 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     * @param password
     */

    @RequestMapping(value="/loginWithThirdThree", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第三步。（共三步）。-1直接提示消息；")
    public void loginWithThirdThree(HttpServletRequest request,
                                    HttpServletResponse response,
                                    @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                    @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                    @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                    @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                    @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                    @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
                                    @ApiParam(required=true, name="password", value="密码（明文）")@RequestParam(value = "code", required = true)String password){

        try{

            SundayWeixinMemberDto member= memberService.loginWithThirdThree( thirdType, nickName, image,  unionId,  mobile, code, password);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版和管理版】退出登录。-1直接提示消息；
     * @param request
     * @param response
     */

    @RequestMapping(value="/logout", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】退出登录。-1直接提示消息；")
    public void logout(HttpServletRequest request,
                       HttpServletResponse response){

        try{
            Long memberId = HttpRequestUtil.getUser(request).getId();
            memberService.logout(memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 【用户版和管理版】修改个人信息。-1直接提示消息；
     * @param request
     * @param response
     * @param nickName
     * @param mobileName
     * @param address
     * @param email
     * @param image
     * @param idCard
     * @param desc
     */

    @RequestMapping(value="/update", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改个人信息。-1直接提示消息；会返回新的token，记得更新移动端的token")
    public void update(HttpServletRequest request,
                       HttpServletResponse response,

                       @ApiParam(required=false, name="nickName", value="昵称")@RequestParam(value = "nickName", required = false)String nickName,
                       @ApiParam(required=false, name="mobileName", value="联系人")@RequestParam(value = "mobileName", required = false)String mobileName,
                       @ApiParam(required=false, name="address", value="地址")@RequestParam(value = "address", required = false)String address,
                       @ApiParam(required=false, name="email", value="电子邮箱")@RequestParam(value = "email", required = false)String email,
                       @ApiParam(required=false, name="image", value="头像")@RequestParam(value = "image", required = false)String image,
                       @ApiParam(required=false, name="idCard", value="身份证号码")@RequestParam(value = "idCard", required = false)String idCard,
                       @ApiParam(required=false, name="desc", value="个人简介")@RequestParam(value = "desc", required = false)String desc) {

        try {
            Long memberId = HttpRequestUtil.getUser(request).getId();
            SundayWeixinMemberDto result = memberService.update(memberId, nickName, mobileName, address, email, image, idCard, desc);
            ResponseMessage.success(result);
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
}
